//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright 2007 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   CATALOG_DTL_TEMPLATE
// AUTHOR           :   Craig Chun
// DATE CREATE      :   2007/07/20
// DESCRIPTION      :   a DAO file for the function, CATALOG_DTL_TEMPLATE
//                  :
//                  :
//                  :
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2007/07/20   Craig Chun      1.0                 source is generated.
//
//
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package com.erp.app.setup.itemDetailTemplate.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.erp.app.common.ProjectConstants;
import com.erp.app.setup.itemDetailTemplate.criteria.CatalogDtlTemplateSrchCriteria;
import com.erp.app.setup.itemDetailTemplate.pk.CatalogDtlTemplatePk;
import com.erp.app.setup.itemDetailTemplate.vo.CatalogDtlTemplateVo;

import com.erp.framework.exceptions.FindRecordException;
import com.erp.framework.model.criteria.SimpleBaseSrchCriteria;
import com.erp.framework.model.criteria.SimplePageEditStyleSrchCriteria;
import com.erp.framework.model.dao.SimplePageEditStyleDao;
import com.erp.framework.model.pk.SimpleBasePk;
import com.erp.framework.model.pk.SimplePageEditStylePk;
import com.erp.framework.model.vo.ChildList;
import com.erp.framework.model.vo.SimpleBaseVo;
import com.erp.framework.model.vo.SimplePageEditStyleVo;
import com.erp.framework.types.CustomBigDecimal;
import com.erp.framework.types.CustomBigInteger;
import com.erp.framework.types.CustomSqlDate;
import com.erp.framework.types.CustomTimestamp;
import com.erp.framework.types.CustomUtilDate;
import com.erp.framework.util.DateTimeUtil;
import com.erp.framework.util.NumberUtil;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.logger.Logger;

/**
* <p>A <strong>DAO</strong>(Data Access Object) abstracts and encapsulates
* all access to the persistent store.  The DAO manages the connection with the
* data source to obtain and store data.  Also, the DAO implements
* the access mechanism required to work with the data source,
* and always provides a uniform API to its clients.
*
* @author          Craig Chun
* @version 1.0     2007/07/20
* @since           FrameWork 1.0
*
*/
public class CatalogDtlTemplateDao extends SimplePageEditStyleDao {

	/**
	* Returns the SQL select statement.
	* @param         obj     the searching criteria
	* @return        a string containing sql query
	*
	* @author     Craig Chun
	* @since      Framework 1.0
	*/
	protected String sqlQuery(SimpleBaseSrchCriteria obj) {
		String sql = CatalogDtlTemplateCommonDao.getHdrSqlQuery(obj);

		return sql;
	}


	/**
	* Returns the SQL select statement.
	* @return        a string containing sql query
	*
	* @author     Craig Chun
	* @since      Framework 1.0
	*/
	protected String sqlSelectByPrimaryKey(){

		return sqlFind();
	}

	/**
	* Sets the values of a result set to a pk
	* @param         pStmt   the incoming prepared statement
	* @param         pk      the searching primary key
	* @return        a vo containing the result set values
	*
	* @author     Craig Chun
	* @since      Framework 1.0
	*/
	protected void setValuesForSelectByPrimaryKey(PreparedStatement pStmt, SimpleBasePk pk) throws SQLException{

		setValuesForFind(pStmt, pk);
	}

	/**
	* Returns the constructed model from a result set.
	* @param         rs     the incoming result set
	* @return        a constructed VO from the result set
	*
	* @author     Craig Chun
	* @since      Framework 1.0
	*/
	protected SimpleBaseVo getModelForSelectByPrimarKey(ResultSet rs) throws SQLException {

		return getResultSetValues(rs);
	}


	/**
	* Returns the SQL select statement.
	* @param         criteria     the searching criteria
	* @return        a string containing sql query
	*
	* @author     Craig Chun
	* @since      Framework 1.0
	*/
	protected String sqlSelectByCriteria(SimpleBaseSrchCriteria criteria) {

		return sqlQuery(criteria);
	}

	/**
	* Returns the constructed model from a result set.
	* @param         rs     the incoming result set
	* @return        a constructed VO from the result set
	*
	* @author     Craig Chun
	* @since      Framework 1.0
	*/
	protected SimpleBaseVo getModelForSelectByCriteria(ResultSet rs) throws SQLException {

		return getResultSetValues(rs);
	}

	/**
	* Returns the constructed primary key from a result set.
	* @param         rs     the incoming result set
	* @return        a constructed PK from the result set
	*
	* @author     Craig Chun
	* @since      Framework 1.0
	*/
	protected SimpleBasePk getPkForSelectByCriteria(ResultSet rs) throws SQLException {

		CatalogDtlTemplatePk pk = new CatalogDtlTemplatePk(rs.getString("COMPANY_CODE"), CustomBigInteger.customize(rs.getBigDecimal("NODE_ID")), CustomBigInteger.customize(rs.getBigDecimal("DTL_ID")));
		return pk;
	}


	/**
	* Returns an array list of child VOs by the parent
	* @param         parent   the incoming parent VO
	* @param         conn     the database connection
	* @return        an array list of child VOs
	*
	* @author     Craig Chun
	* @since      Framework 1.0
	*/
	public ChildList selectByParent(SimpleBaseVo parent, Connection conn)
		throws FindRecordException {

		return null;
	}

	/**
	* Constructs a string of SQL insert statement
	* @return        a string of SQL insert statement
	*
	* @author     Craig Chun
	* @since      Framework 1.0
	*/
	protected String sqlInsert() {

		String sql = "INSERT INTO T_CATALOG_DTL_TEMPLATE ("
			+ "COMPANY_CODE, NODE_ID, DTL_ID, SEQ, STATUS, "
			+ "BROWSER_COLUMN) "
			+ "VALUES ("
			+ "?, ?, ?, ?, ?, "
			+ "?)";
		return sql;
	}

	/**
	* Sets the values for a SQL insert or update statement.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming VO
	*
	* @author     Craig Chun
	* @since      Framework 1.0
	*/
	protected void setValuesForInsertAndModify(PreparedStatement pStmt, Object obj, String opType)
		throws SQLException {

		CatalogDtlTemplateVo vo = (CatalogDtlTemplateVo) obj;
		int i = 1;

		pStmt.setString(i++, vo.getCompanyCode());
		setNullableBigInteger(pStmt,i++, vo.getNodeId());
		setNullableBigInteger(pStmt,i++, vo.getDtlId());
		setNullableBigInteger(pStmt, i++, vo.getHdrSeq());
		setNullableString(pStmt, i++, vo.getHdrStatus());
		setNullableBigInteger(pStmt, i++, vo.getHdrBrowserColumn());

		if(ProjectConstants.DB_OP_UPDATE.equals(opType)){
			pStmt.setString(i++, vo.getCompanyCode());
			pStmt.setInt(i++, vo.getNodeId().intValue());
			pStmt.setInt(i++, vo.getDtlId().intValue());
		}
	}

	/**
	* Sets the values for a SQL insert statement.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming VO
	*
	* @author     Craig Chun
	* @since      Framework 1.0
	*/
	protected void setValuesForInsert(PreparedStatement pStmt, Object obj)
		throws SQLException {

		setValuesForInsertAndModify(pStmt, obj, ProjectConstants.DB_OP_INSERT);
	}

	/**
	* Constructs a string of SQL update statement
	* @return        a string of SQL update statement
	*
	* @author     Craig Chun
	* @since      Framework 1.0
	*/
	protected String sqlUpdate() {

		String sql = "UPDATE T_CATALOG_DTL_TEMPLATE SET COMPANY_CODE=?, NODE_ID=?, DTL_ID=?, SEQ=?, STATUS=?, "
			+ "BROWSER_COLUMN=? "
			+ "WHERE COMPANY_CODE=? AND NODE_ID=? AND DTL_ID=? ";
		return sql;
	}

	/**
	* Sets the values for a SQL update statement.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming VO
	*
	* @author     Craig Chun
	* @since      Framework 1.0
	*/
	protected void setValuesForModify(PreparedStatement pStmt, Object obj)
		throws SQLException {

		setValuesForInsertAndModify(pStmt, obj, ProjectConstants.DB_OP_UPDATE);
	}

	/**
	* Constructs a string of SQL delete statement
	* @return        a string of SQL update statement
	*
	* @author     Craig Chun
	* @since      Framework 1.0
	*/
	protected String sqlDelete() {
		String sql = "DELETE FROM T_CATALOG_DTL_TEMPLATE WHERE COMPANY_CODE=? AND NODE_ID=? AND DTL_ID=? ";

		return sql;
	}

	/**
	* Sets the values for a SQL delete statement.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming VO
	*
	* @author     Craig Chun
	* @since      Framework 1.0
	*/
	protected void setValuesForDelete(PreparedStatement pStmt, Object obj)
		throws SQLException {

		CatalogDtlTemplateVo vo = (CatalogDtlTemplateVo) obj;
		int i = 1;

		pStmt.setString(i++, vo.getCompanyCode());
		pStmt.setInt(i++, vo.getNodeId().intValue());
		pStmt.setInt(i++, vo.getDtlId().intValue());
	}

	/**
	* Constructs a string of SQL select statement
	* @return        a string of SQL select statement
	*
	* @author     Craig Chun
	* @since      Framework 1.0
	*/
	protected String sqlFind() {

		String sql = "SELECT COMPANY_CODE, NODE_ID, DTL_ID, SEQ, STATUS, "
			+ "BROWSER_COLUMN "
			+ "FROM T_CATALOG_DTL_TEMPLATE "
			+ "WHERE COMPANY_CODE=? AND NODE_ID=? AND DTL_ID=? "
			+ "ORDER BY COMPANY_CODE, NODE_ID, DTL_ID";
		return sql;
	}

	/**
	* Sets the values for a SQL select statement for finding by primary key.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming PK
	*
	* @author     Craig Chun
	* @since      Framework 1.0
	*/
	protected void setValuesForFind(PreparedStatement pStmt, Object obj)
		throws SQLException {

		CatalogDtlTemplatePk pk = (CatalogDtlTemplatePk) obj;
		int i = 1;

		pStmt.setString(i++, pk.getCompanyCode());
		pStmt.setInt(i++, pk.getNodeId().intValue());
		pStmt.setInt(i++, pk.getDtlId().intValue());
	}

	/**
	* Constructs a string of SQL select statement
	* @return        a string of SQL select statement
	*
	* @author     Craig Chun
	* @since      Framework 1.0
	*/
	protected String sqlList() {

		return null;
	}

	/**
	* Sets the values for a SQL select statement for finding child tables.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming parent PK
	*
	* @author     Craig Chun
	* @since      Framework 1.0
	*/
	protected void setValuesForList(PreparedStatement pStmt, Object obj)
		throws SQLException {

	}

	/**
	* Sets the values of a result set to a vo for a search screen
	* @param         rs   the incoming result set
	* @return        a vo containing the result set values
	*
	* @author     Craig Chun
	* @since      Framework 1.0
	*/
	protected SimpleBaseVo getSearchResultSetValues(ResultSet rs) throws SQLException {


		return CatalogDtlTemplateCommonDao.getHdrSearchResultSetValues(rs);
	}

	/**
	* Sets the values of a result set to a vo
	* @param         rs   the incoming result set
	* @return        a vo containing the result set values
	*
	* @author     Craig Chun
	* @since      Framework 1.0
	*/
	protected SimpleBaseVo getResultSetValues(ResultSet rs) throws SQLException {

		CatalogDtlTemplateVo vo = new CatalogDtlTemplateVo();

		vo.setCompanyCode(rs.getString("COMPANY_CODE"));
		vo.setNodeId(CustomBigInteger.customize(rs.getBigDecimal("NODE_ID")));
		vo.setDtlId(CustomBigInteger.customize(rs.getBigDecimal("DTL_ID")));
		vo.setHdrSeq(CustomBigInteger.customize(rs.getBigDecimal("SEQ")));
		vo.setHdrStatus(rs.getString("STATUS"));
		vo.setHdrBrowserColumn(CustomBigInteger.customize(rs.getBigDecimal("BROWSER_COLUMN")));
		return vo;
	}

}
